package com.controller;

import java.util.Base64;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import comDo.LoginService;
import comDo.dao.ModeUser;
import comDo.dao.ModeUserDao;
import modeEntity.UserPojo;

@Controller
public class UserController {
	final Base64.Decoder decoder = Base64.getDecoder();
	@Autowired
	private ModeUserDao modeUserDao;
	@Autowired
	private LoginService loginservice;
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public String login( HttpServletRequest request ,@RequestBody UserPojo user){
		String name = new String(decoder.decode(user.getName()));
		String password = new String(decoder.decode(user.getPassword()));
	    ModeUser modeUser =	modeUserDao.selectByNameAndPassword(name, password);
	    JSONObject json = new JSONObject();
	    if(modeUser != null){
	    	json.put("isOk", true);
	    	HttpSession session =request.getSession();
	    	session.setAttribute("user", modeUser);
	    }else{
	    	json.put("isOk", false);
	    }
		return json.toJSONString();
	}
	@RequestMapping(value="/deal",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String deal(){
		return loginservice.deal();
	}
}
